package cn.myallways.iglp.web.controller.mdm;


import com.allways.dubbo.mdm.beans.dealer.DealerEntity;
import com.allways.dubbo.mdm.exception.MdmException;
import com.allways.dubbo.mdm.service.dealer.IDealerService;
import com.myallways.iglp.cc.web.entity.ReturnVO;
import com.myallways.iglp.framework.base.controller.BaseController;
import com.myallways.iglp.framework.base.pagination.DataTableRequest;
import com.myallways.iglp.framework.base.pagination.DataTableResponse;
import com.myallways.iglp.framework.base.pagination.PageInfo;
import com.myallways.iglp.framework.base.pagination.PaginationResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 经销商信息Controller
 * Created by weih on 2016-09-29.
 */
@Controller
@RequestMapping("/mdm/dealer")
public class DealerController extends BaseController {

    @Autowired
    IDealerService dealerService;
    /**
     * 跳转新增经销商页面
     * @return
     */
    @RequestMapping("/toAdd")
    public String toAddPage(){
        return "mdm/dealer/add";
    }
    /**
     * 新增经销商信息
     * @param dealer 经销商对象
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public void addDealer(@RequestBody Dealer dealer,HttpServletResponse response){
        ReturnVO vo = new ReturnVO();
        vo.setStatus(HttpServletResponse.SC_OK);
        dealer.setCreateUser(getSessionUser().getUserId());
        dealer.setDataSource(BusiConstants.DATASOUCE_CODE_FOR_MDM);
        try {
            dealerService.addDealer(dealer);
        }catch (MdmException e){
            vo.setStatus(e.getCode());
            vo.setMessage(e.getMessage());
        }
        super.ajaxJson(response, vo);
    }

    /**
     * 跳转编辑经销商页面
     * @param dealerCode  经销商代码
     * @return
     */
    @RequestMapping("/toEdit")
    public String toEditPage(String dealerCode,Model model){
        model.addAttribute("dealer", dealerService.getDealerByCode(dealerCode));
        return "mdm/dealer/edit";
    }

    /**
     * 编辑经销商信息
     * @param dealer
     * @return
     */
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public void editDealer(@RequestBody Dealer dealer,HttpServletResponse response){
        ReturnVO vo = new ReturnVO();
        vo.setStatus(HttpServletResponse.SC_OK);
        Dealer oldDealer=dealerService.getDealerByCode(dealer.getDealerCode());
        oldDealer.setDealerId(dealer.getDealerId());
        oldDealer.setCompanyCode(dealer.getCompanyCode());
        oldDealer.setDealerCode(dealer.getDealerCode());
        oldDealer.setDealerName(dealer.getDealerName());
        oldDealer.setNationCode(dealer.getNationCode());
        oldDealer.setProvinceCode(dealer.getProvinceCode());
        oldDealer.setCityCode(dealer.getCityCode());
        oldDealer.setDistrictCode(dealer.getDistrictCode());
        oldDealer.setStreet(dealer.getStreet());
        oldDealer.setAddress(dealer.getAddress());
        oldDealer.setPostcode(dealer.getPostcode());
        oldDealer.setContact(dealer.getContact());
        oldDealer.setMobiPhone(dealer.getMobiPhone());
        oldDealer.setTel(dealer.getTel());
        oldDealer.setFax(dealer.getFax());
        oldDealer.setLatitude(dealer.getLatitude());
        oldDealer.setLongitude(dealer.getLongitude());
        oldDealer.setDisplay(dealer.getDisplay());
        oldDealer.setUpdateUser(getSessionUser().getUserId());
        oldDealer.setDataSource(BusiConstants.DATASOUCE_CODE_FOR_MDM);
        try {
            dealerService.updateDealer(oldDealer);
        }catch (MdmException e){
            vo.setStatus(e.getCode());
            vo.setMessage(e.getMessage());
        }
        super.ajaxJson(response,vo);
    }

    /**
     * 删除经销商信息
     * @param dealer 经销商对象
     * @return
     */
    @RequestMapping(value= "/delete", method = RequestMethod.POST)
    public void deleteDealer(@RequestBody Dealer dealer,HttpServletResponse response){
        ReturnVO vo = new ReturnVO();
        vo.setStatus(HttpServletResponse.SC_OK);
        dealer.setUpdateUser(getSessionUser().getUserId());
        dealer.setDataSource(BusiConstants.DATASOUCE_CODE_FOR_MDM);
        try {
            dealerService.deleteDealer(dealer);
        }catch (MdmException e){
            vo.setStatus(e.getCode());
            vo.setMessage(e.getMessage());
        }
        super.ajaxJson(response, vo);
    }

    /**
     * 跳转分页查询界面
     * @return
     */
    @RequestMapping("/list")
    public String toListPage(){
        return "mdm/dealer/list";
    }

    /**
     * 分页查询数据
     * @param dealer 经销商对象
     * @param dtrequest 页码
     */
    @RequestMapping("/getListData")
    @ResponseBody
    public DataTableResponse<Dealer> getDealerData(Dealer dealer ,DataTableRequest dtrequest){
        int cnt = (int)dealerService.getDealerCount(dealer);
        List<Dealer> list = dealerService.queryDealers(dealer, dtrequest.getiDisplayStart(), dtrequest.getiDisplayStart() + dtrequest.getiDisplayLength());
        PageInfo pi = new PageInfo();

        pi.setTotal(cnt);
        pi.setRows(dtrequest.getiDisplayLength());
        pi.setPage(dtrequest.getCurrentPage());
        PaginationResult<Dealer> result = new PaginationResult<Dealer>(list, pi);
        return new DataTableResponse<Dealer>(dtrequest.getsEcho(),result);
    }

}
